package com.flaremars.picker.views;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.flaremars.picker.R;
import com.flaremars.picker.utils.ScreenUtils;

public class NavigationBarView extends LinearLayout implements OnClickListener {

	public NavigationBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public NavigationBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public NavigationBarView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context){
		setOrientation(LinearLayout.HORIZONTAL);
//		setBackgroundColor(Color.parseColor("#fcfcfc"));
        setBackgroundResource(R.drawable.bg_tags_bar);
		LayoutInflater inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.navigation_bar, this);
        setPadding(0, ScreenUtils.INSTANCE.dp2px(context,4.0f),0,0);
	}


	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		one = (BottomSingleView) findViewById(R.id.nav_one);
		two = (BottomSingleView) findViewById(R.id.nav_two);
        three = (BottomSingleView) findViewById(R.id.nav_three);


		one.setOnClickListener(this);
		two.setOnClickListener(this);
        three.setOnClickListener(this);

        one.setProgress(0);
		two.setProgress(100);
        three.setProgress(0);
	}

	private ViewPager mViewPager ;
	public void setViewPager(ViewPager mViewPager){
		this.mViewPager = mViewPager ;
		this.mViewPager.addOnPageChangeListener(listener);
	}


	private OnPageChangeListener listener = new OnPageChangeListener(){
		@Override
		public void onPageScrollStateChanged(int arg0) {
			if (arg0 == 0 ) {
				isClick = false ;
			}
		}
		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {
			if (!isClick) {
				setChildProgress(position, 1 - positionOffset);
				setChildProgress(position + 1, positionOffset);
			}
		}
		@Override
		public void onPageSelected(int arg0) {
			currentID = arg0  ;
		}
	};


	private  void setChildProgress(int position , float progress){
		switch (position) {
			case 0 :
				one.setProgress(progress);
				break;
			case 1:
				two.setProgress(progress);
				break;
            case 2:
                three.setProgress(progress);
                break;
		}
	}
	private BottomSingleView one;
	private BottomSingleView two;
	private BottomSingleView three;

	private int currentID = 0 ;


	private boolean isClick = false ;
	@Override
	public void onClick(View v) {
		isClick = true ;
		restoreState(currentID);
		switch (v.getId()) {
			case R.id.nav_one:
				one.setProgress(1);
				currentID = 0 ;
				mViewPager.setCurrentItem(0);
				break;
			case R.id.nav_two:
				two.setProgress(1);
				currentID = 1 ;
				mViewPager.setCurrentItem(1);
				break;
			case R.id.nav_three:
                three.setProgress(1);
				currentID = 2 ;
				mViewPager.setCurrentItem(2);
				break;
		}
	}

	private void restoreState(int lastPosition){
		switch (lastPosition) {
			case 0:
				one.setProgress(0);
				break;
			case 1:
				two.setProgress(0);
				break;
            case 2:
                three.setProgress(0);
                break;
		}
	}

}
